//
//  STBView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/29.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "STBView.h"

@interface STBView () <UIScrollViewDelegate>
{
    
}

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation STBView

- (void)setButton:(UIButton *)button WithImageName:(NSString *)name
{
    UIImage *image = GET_IAMGE_WITH_NAME(name);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, 30, 30)];
    imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 2);
    [button addSubview:imageView];
    imageView = nil;
}

- (void)setButton:(UIButton *)button WithImageName:(NSString *)name Title:(NSString *)title
{
    UIImage *image = GET_IAMGE_WITH_NAME(name);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, 30, 30)];
    imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 3);
    [button addSubview:imageView];
    imageView = nil;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    label.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 3 * 2);
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    [button addSubview:label];
    label = nil;
}

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.delegate = self;
        _scrollView.backgroundColor = BACKGROUND_COLOR;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}

- (void)addHead
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.bounds.size.width, self.bounds.size.height / 5)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    leftButton.center = CGPointMake(35, view.bounds.size.height / 2);
    [leftButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnon") forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(pressSTBOn) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    rightButton.center = CGPointMake(view.bounds.size.width - 35, view.bounds.size.height / 2);
    [rightButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnoff") forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(pressSTBOff) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 - 5, 100, 20)];
    label1.text = @"开启机顶盒";
    label1.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label1.textColor = [UIColor blackColor];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 + 20, 150, 40)];
    label2.text = @"只有开启机顶盒才可以控制";
    label2.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    label2.textColor = [UIColor lightGrayColor];
    [label2 setLineBreakMode:NSLineBreakByWordWrapping];
    label2.numberOfLines = 0;
    
    [view addSubview:leftButton];
    [view addSubview:rightButton];
    [view addSubview:label1];
    [view addSubview:label2];
    [self.scrollView addSubview:view];
    leftButton = nil;
    rightButton = nil;
    label1 = nil;
    label2 = nil;
    view = nil;
}

- (void)addFundation
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5 + 25, 50, 30)];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.text = @"操作";
    label.textColor = TEXT_GRAY_COLOR;
    [self.scrollView addSubview:label];
    label = nil;
    UIButton *button;
    for(int i = 0; i < 12; i++){
        button = [[UIButton alloc] initWithFrame:CGRectMake((i % 3) * ((self.bounds.size.width - 3) / 4.0 + 1),
                                                           (i / 3) * ((self.bounds.size.height / 10.0) + 1) + self.bounds.size.height / 5 + 65,
                                                            (self.bounds.size.width - 3) / 4.0, (self.bounds.size.height / 10.0))];
        button.backgroundColor = [UIColor whiteColor];
        button.tag = i;
        if(i == 0){
            [button setTitle:@"退出" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }else if(i == 1){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_up"];
        }else if(i == 2){
            [button setTitle:@"静音" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }else if(i == 3){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_left"];
        }else if(i == 4){
            [button setTitle:@"确认" forState:UIControlStateNormal];
            [button setTitleColor:TEXT_GREEN_COLOR forState:UIControlStateNormal];
        }else if(i == 5){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_right"];
        }else if(i == 6){
            [button setTitle:@"CH-" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        }else if(i == 7){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_down"];
        }else if(i == 8){
            [button setTitle:@"CH+" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        }else if(i == 9){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_return"];
        }else if(i == 10){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_back"];
        }else{
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_menu"];
        }
        [button addTarget:self action:@selector(pressFundationButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    
    for(int j = 0; j < 2; j++){
        button = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - (self.bounds.size.width - 3) / 4,
                                                           self.bounds.size.height / 5 + 65 + j * (self.bounds.size.height / 5 + 2),
                                                           (self.bounds.size.width - 3) / 4.0,
                                                            self.bounds.size.height / 5 + 1)];
        button.backgroundColor = [UIColor whiteColor];
        button.tag = 12 + j;
        if(j == 0){
            [self setButton:button WithImageName:@"Image/fundationView/TV/volume_up_p" Title:@"音量+"];
        }else{
            [self setButton:button WithImageName:@"Image/fundationView/TV/volume_down_p" Title:@"音量-"];
        }
        [button addTarget:self action:@selector(pressFundationButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    button = nil;
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height / 5 * 3 + 68);
}

#pragma mark - 按钮响应函数
- (void)pressSTBOn
{
    //开
    [self.delegate sendRespondByFuctionName:STB_ON];
}

- (void)pressSTBOff
{
    //关
    [self.delegate sendRespondByFuctionName:STB_OFF];
}

- (void)pressFundationButton:(UIButton *)button
{
    if(button.tag == 0){
        //退出
        [self.delegate sendRespondByFuctionName:STB_EXIT];
    }else if (button.tag == 1){
        //上
        [self.delegate sendRespondByFuctionName:STB_UP];
    }else if (button.tag == 2){
        //静音
        [self.delegate sendRespondByFuctionName:STB_QUITE];
    }else if (button.tag == 3){
        //左
        [self.delegate sendRespondByFuctionName:STB_LEFT];
    }else if (button.tag == 4){
        //确认
        [self.delegate sendRespondByFuctionName:STB_OK];
    }else if (button.tag == 5){
        //右
        [self.delegate sendRespondByFuctionName:STB_RIGHT];
    }else if (button.tag == 6){
        //CH-
        [self.delegate sendRespondByFuctionName:STB_CH_DOWN];
    }else if (button.tag == 7){
        //下
        [self.delegate sendRespondByFuctionName:STB_DOWN];
    }else if (button.tag == 8){
        //CH+
        [self.delegate sendRespondByFuctionName:STB_CH_UP];
    }else if (button.tag == 9){
        //
        [self.delegate sendRespondByFuctionName:STB_BACK];
    }else if (button.tag == 10){
        //
        [self.delegate sendRespondByFuctionName:STB_BACKOFF];
    }else if (button.tag == 11){
        //菜单
        [self.delegate sendRespondByFuctionName:STB_MENU];
    }else if (button.tag == 12){
        //音量+
        [self.delegate sendRespondByFuctionName:STB_VOL_UP];
    }else{
        //音量-
        [self.delegate sendRespondByFuctionName:STB_VOL_DOWN];
    }
}

- (instancetype) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.scrollView];
        [self addHead];
        [self addFundation];
    }
    
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
